package com.zendesk.maxwell.util;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.concurrent.ThreadFactory;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @Auther: miaoguoxin
 * @Date: 2021/12/24 14:48
 * @Description:
 */
public class NamedThreadFactory implements ThreadFactory {
	private static final Logger LOGGER = LoggerFactory.getLogger(NamedThreadFactory.class);
	final private static String DEFAULT_NAME = "maxwell-worker";
	final private String name;
	final private boolean daemon;
	final private ThreadGroup group;
	final private AtomicInteger threadNumber = new AtomicInteger(0);
	final static Thread.UncaughtExceptionHandler uncaughtExceptionHandler = (t, e) -> {
		if (e instanceof InterruptedException
				|| (e.getCause() != null && e.getCause() instanceof InterruptedException)) {
			return;
		}

		LOGGER.error("from " + t.getName(), e);
	};

	public NamedThreadFactory() {
		this(DEFAULT_NAME, true);
	}

	public NamedThreadFactory(String name) {
		this(name, true);
	}

	public NamedThreadFactory(String name, boolean daemon) {
		this.name = name;
		this.daemon = daemon;
		SecurityManager s = System.getSecurityManager();
		group = (s != null) ? s.getThreadGroup() : Thread.currentThread().getThreadGroup();
	}

	@Override
	public Thread newThread(Runnable r) {
		Thread t = new Thread(group, r, name + "-" + threadNumber.getAndIncrement(), 0);
		t.setDaemon(daemon);
		if (t.getPriority() != Thread.NORM_PRIORITY) {
			t.setPriority(Thread.NORM_PRIORITY);
		}

		t.setUncaughtExceptionHandler(uncaughtExceptionHandler);
		return t;
	}
}
